home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6561 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: accessing structures (newbie question)
  5. Date: 19 Feb 1996 15:04:00 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4ga3h0$h20@sparcserver.lrz-muenchen.de>
  9. References: <4g8gic$o6u@news1.sunbelt.net>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. dking@SunBelt.Net writes:
  13.  
  14. >Ok. having trouble accessing structure. here I define a pointer and malloc the 
  15. >memory:
  16.  
  17. You don't have problems accessing a struct, you have problems
  18. accessing an array.
  19.  
  20. >struct picture *photos;
  21. >photos = (struct picture *)malloc(1000 * sizeof(picture));
  22.  
  23. >now I try to store data into structures...
  24.  
  25. >    for (x=0;x<num_of_files;x++)
  26. >    {
  27. >        strncpy(photos->filename, filelist[x],12);
  28. >        strncpy(photos->diskname, inputdisk,12);
  29. >        strncpy(photos->indexname, inputindex,12);
  30. >    }
  31.  
  32. Why do you allocate space for 1000 pictures and use "num_of_files" of
  33. them? To access the "x"th element of an array, it is customary to
  34. use syntax like
  35.  
  36.    strncpy(photos[x].filename, filelist[x], 12);
  37.  
  38. You do it for "filelist", so why don't you want to do it for "photos",
  39. too?
  40.  
  41. >now I try to read data...
  42.  
  43.  
  44. >for (x=0;x<num_of_files;x++)
  45. >{
  46. >   
  47. >printf("%sdisk%sindex%s\n",photos->filename,photos->diskname,photos->indexname
  48. >;    }
  49.  
  50. >Obviously I'm pointing to the first of the 1000 structures the entire time. 
  51. >Now for the 60,000 dollar question: HOW do I access the rest of the 
  52. >structures? My tutuorial doesnt have any examples of accessing  MALLOCed 
  53. >structures. The one example on structures defines it as an array (struct 
  54. >picture *photos[x] ) but 1000 elements is too large an array and the example 
  55. >with malloc uses floating point data accessed by using (photos[x]) which 
  56. >doesnt work either with the strnccpy or with the -> operator.
  57.  
  58. Well, "photos[x]" is equivalent to "*(photos + x)", so if "photos" is
  59. a pointer to a "struct picture", then "photos[x]" is a plain "struct
  60. picture", not a pointer to one. This is why the "->" operator will not
  61. work for "photos[x]". You have to fall back to ".". You could, of course,
  62. do something like:
  63.  
  64.    struct picture *photos, *p;
  65.    photos = (struct picture *)malloc(num_files * sizeof(picture));
  66.  
  67.    for (x=0, p = photos; x<num_of_files; x++, p++)
  68.    {
  69.       strncpy(p->filename, filelist[x],12);
  70.       strncpy(p->diskname, inputdisk,12);
  71.    }
  72.  
  73. Kurt
  74. --
  75. | Kurt Watzka                             Phone : +49-89-2180-6254
  76. | watzka@stat.uni-muenchen.de
  77. | ua302aa@sunmail.lrz-muenchen.de
  78.  
  79.